package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ArticleDAO;
import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.domain.Article;
import bist.believeinstep.mapper.ArticleMapper;
import bist.believeinstep.util.Constant.ArticleType;
import bist.believeinstep.util.Constant.State;

public class ArticleTemplate implements ArticleDAO, ConnectionDAO{
	
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void setDataSource(DataSource dataSource) {
		
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Override
	public boolean create(Article article) {
	
		String query = "INSERT INTO bist_articles(code, title, introduction, content, type, state, avatar, created_date, modified_date, created_by, modified_by)" +
				" VALUES(?,?,?,?,?,?,?,?,?,?,?)";

		try {
			jdbcTemplate.update(query, new Object[]{
					article.getCode(),
					article.getTitle(),
					article.getIntroduction(),
					article.getContent(),
					article.getType().name(),
					article.getState().name(),
					article.getAvatar(),
					article.getCreated(),
					article.getModified(),
					article.getCreatedBy(),
					article.getModifiedBy()
			});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean update(Article article) {
		String query = "UPDATE bist_articles" +
				" SET title = ?" +
				"introduction = ?" +
				"content = ?" +
				"type = ?" +
				"state = ?" +
				"avatar = ?" +
				"date_modified = ?" +
				"modified_by = ?" +
				" WHERE id = ?";
		try {
			jdbcTemplate.update(query, new Object[]{
					article.getTitle(),
					article.getIntroduction(),
					article.getContent(),
					article.getType().name(),
					article.getState().name(),
					article.getAvatar(),
					article.getModified(),
					article.getModifiedBy(),
					article.getId()
			});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(int id) {
		String query = "DELETE bist_articles WHERE id = ?";
		try {
			jdbcTemplate.update(query, new Object[]{id});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(String code) {
		String query = "DELETE bist_articles WHERE code = ?";
		try {
			jdbcTemplate.update(query, new Object[]{code});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Article find(int id) {
		String query = "SELECT * FROM bist_articles WHERE id = ?";
		try {
			Article article = jdbcTemplate.queryForObject(query, new Object[]{id}, new ArticleMapper());
			return article;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Article find(String code) {
		String query = "SELECT * FROM bist_articles WHERE code = ?";
		try {
			Article article = jdbcTemplate.queryForObject(query, new Object[]{code}, new ArticleMapper());
			return article;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Article> find(ArticleType type) {
		String query = "SELECT * FROM bist_articles WHERE type = ?";
		try {
			List<Article> articles = jdbcTemplate.query(query, new Object[]{type.name()}, new ArticleMapper());
			return articles;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Article> find(State state) {
		String query = "SELECT * FROM bist_articles WHERE state = ?";
		try {
			List<Article> articles = jdbcTemplate.query(query, new Object[]{state.name()}, new ArticleMapper());
			return articles;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Article> findAll() {
		String query = "SELECT * FROM bist_articles";
		try {
			List<Article> articles = jdbcTemplate.query(query, new ArticleMapper());
			return articles;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
